package com.pic4493.app;

import org.json.JSONArray;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.pic4493.base.BaseUITopBar;
import com.pic4493.comm.Session;
import com.pic4493.entities.DJson;
import com.pic4493.entities.EUser;
import com.pic4493.net.NetClientJ;
import com.pic4493.utils.UtiDialog;
import com.pic4493.utils.UtiMD5;

public class UILogin extends BaseUITopBar implements OnClickListener
{

   private LinearLayout mLayPwdAg;
   private LinearLayout mLayNickName;
   private CheckBox mCkbIsSignUp;
   private Button mBtnLogin;
   private Button mBtnSignUpLogin;
   private EditText mEdtUserName;
   private EditText mEdtNickName;
   private EditText mEdtPwd;
   private EditText mEdtPwdAg;

   private void setLogin(boolean isLogin)
   {
	  if (isLogin)
	  {
		 mLayPwdAg.setVisibility(View.GONE);
		 mLayNickName.setVisibility(View.GONE);
		 mBtnLogin.setVisibility(View.VISIBLE);
		 mBtnSignUpLogin.setVisibility(View.GONE);
	  }
	  else
	  {
		 mLayNickName.setVisibility(View.VISIBLE);
		 mLayPwdAg.setVisibility(View.VISIBLE);
		 mBtnLogin.setVisibility(View.GONE);
		 mBtnSignUpLogin.setVisibility(View.VISIBLE);
	  }
   }

   private void initView()
   {
	  mLayPwdAg = (LinearLayout) findViewById(R.id.uilogin_lay_pwd_ag);
	  mLayNickName = (LinearLayout) findViewById(R.id.uilogin_lay_nickname);

	  mCkbIsSignUp = (CheckBox) findViewById(R.id.uilogin_ckb_issignup);
	  mBtnLogin = (Button) findViewById(R.id.uilogin_btn_login);
	  mBtnSignUpLogin = (Button) findViewById(R.id.uilogin_btn_signup_login);
	  mEdtUserName = (EditText) findViewById(R.id.uilogin_edt_username);
	  mEdtPwd = (EditText) findViewById(R.id.uilogin_edt_pwd);
	  mEdtPwdAg = (EditText) findViewById(R.id.uilogin_edt_pwd_ag);
	  mEdtNickName=(EditText)findViewById(R.id.uilogin_edt_nickname);
	  
	  mBtnLogin.setOnClickListener(this);
	  mBtnSignUpLogin.setOnClickListener(this);
	  mCkbIsSignUp.setOnCheckedChangeListener(new OnCheckedChangeListener()
	  {
		 @Override
		 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
		 {
			if (isChecked)
			{
			   setLogin(false);
			}
			else
			{
			   setLogin(true);
			}
		 }
	  });
   }

   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
	  setContentView(R.layout.uilogin);
	  super.onCreate(savedInstanceState);

	  setTitle("登录");
	  initView();
   }

   private EUser BuildUserInfo()
   {

	  try
	  {
		 if (mEdtUserName.getText() == null || mEdtUserName.getText().toString().equals(""))
		 {
			UtiDialog.getInstance().ShowToastInfoShort(UILogin.this, "邮箱不能为空");
			return null;
		 }
		 else if (mEdtPwd.getText() == null || mEdtPwd.getText().toString().equals(""))
		 {
			UtiDialog.getInstance().ShowToastInfoShort(UILogin.this, "密码不能为空");
			return null;
		 }
		 else
		 {
			EUser user = new EUser();
			user.setUserName(mEdtUserName.getText().toString());
			user.setPassWord(UtiMD5.crypt(mEdtPwd.getText().toString()));
			if (mCkbIsSignUp.isChecked())
			{
			   if (mEdtPwdAg.getText() == null || mEdtPwdAg.getText().toString().equals(""))
			   {
				  UtiDialog.getInstance().ShowToastInfoShort(UILogin.this, "请重复输入密码");
				  return null;
			   }
			   else if (!mEdtPwd.getText().toString().equals(mEdtPwdAg.getText().toString()))
			   {
				  UtiDialog.getInstance().ShowToastInfoShort(UILogin.this, "两次密码不符");
				  return null;
			   }
			   else if (mEdtNickName.getText() == null || mEdtNickName.getText().toString().equals(""))
			   {
				  UtiDialog.getInstance().ShowToastInfoShort(UILogin.this, "昵称不能为空");
				  return null;
			   }
			   user.setNickName(mEdtNickName.getText().toString());
			}
			return user;
		 }
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "UILogin_BuildUserInfo() " + e);
	  }
	  return null;
   }

   @Override
   public void onClick(View v)
   {
	  EUser user = BuildUserInfo();
	  if (user != null)
	  {
		 switch (v.getId())
		 {
			case R.id.uilogin_btn_login:
			   executeAsyncTask("login", new Object[] { user });

			   break;
			case R.id.uilogin_btn_signup_login:
			   user = BuildUserInfo();
			   executeAsyncTask("signup", new Object[] { user });
			   break;
		 }
	  }
   }

   @Override
   public void onAsyncTaskStart(String taskName, Object[] params)
   {
	  UtiDialog.getInstance().ShowLoadingDialogNoTitleEX(UILogin.this, "加载中...");
   }

   @Override
   public Object onAsyncTaskInBackground(String taskName, Object[] params) throws Exception
   {
	  if (!taskName.equals(""))
	  {
		 if (taskName.equals("login"))
		 {
			return NetClientJ.Login((EUser) params[0]);
		 }
		 else if (taskName.equals("signup"))
		 {
			return NetClientJ.SignUp((EUser) params[0]);
		 }
	  }
	  return null;
   }

   @Override
   public void onAsyncTaskEnd(String taskId, String taskName, Object[] params, Object result)
   {
	  try
	  {
		 UtiDialog.getInstance().DismissLoadingDialog();
		 if (result != null)
		 {
			DJson data = (DJson) result;
			if (!data.getHead().hasError())
			{
			   if (taskName.equals("login")||taskName.equals("signup"))
			   {
				   EUser user = new EUser();
				   user.setNickName(data.getBody().optString("nickname"));
				   user.setPassWord(data.getBody().optString("passwd"));
				   user.setUserId(data.getHead().getUserId());
				   user.setUserName(data.getBody().optString("username"));
				   user.setNewsCount(data.getBody().optInt("msgCount"));
				   user.setHeadImage(data.getBody().optString("avatars"));
				   Session.getInstance().setUser(user);
			   }
			   
			   InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
			   imm.hideSoftInputFromWindow(mEdtUserName.getWindowToken(), 0);
			   imm.hideSoftInputFromWindow(mEdtPwd.getWindowToken(), 0);
			   imm.hideSoftInputFromWindow(mEdtPwdAg.getWindowToken(), 0);
			   imm.hideSoftInputFromWindow(mEdtNickName.getWindowToken(), 0);
			   setResult(RESULT_OK);
			   finish();
			}
			else
			{
			   UtiDialog.getInstance().ShowToastInfoLong(UILogin.this, data.getHead().getRspMsg());
			}
		 }
		 else
		 {
			UtiDialog.getInstance().ShowToastInfoLong(UILogin.this, "网络异常请稍候再试");
		 }
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "UILogin_onAsyncTaskEnd(taskId, taskName, params, result) " + e);
	  }
   }

}
